package dao;

import bean.ShopBean;
import bean.UserBean;
import com.mysqld.Mysqld;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.JdbcUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDao {

    QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());

    /**
     * 根据当前账号和密码进行查看数据将账号密码传入进去，返回0没有当前账号，返回1有当前账号
     * @param userBean
     * @return
     */
    public int findUser(UserBean userBean){
        UserBean user=null;
        String sql = "select * from s_user where s_account=? and s_password=?";
        try {
            user=qr.query(sql, new BeanHandler<>(UserBean.class),userBean.getAccount(),userBean.getPassword());
        }catch(SQLException e) {
            e.printStackTrace();
        }
        if(user==null){
            return 0;
        }else{
            return 1;
        }
    }

    /**
     * 添加用户，输入账号以及密码
     * @param userBean
     * @return
     */
    public int addUser(UserBean userBean){
        int num=0;
        String sql = "insert s_user (s_account,s_password) VALUES(?,?)";
        try {
            num=qr.update(sql, userBean.getAccount(),userBean.getPassword());
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    /***
     * 传入的是第一页
     * @param star
     * @param page
     * @return
     */
    public List<ShopBean> showShop(String star){
        List<ShopBean> list = null;
        //star是第几页   1页
       int end=Integer.parseInt(star)*8;//结束语
       int sta=end-8;//开始页
        System.out.println("第几页"+star);
        System.out.println("页数"+sta);
        System.out.println("结束"+ end);
        String sql = "SELECT * FROM s_shop LIMIT "+ String.valueOf(sta)+","+"8";
        try {
            list = qr.query(sql, new BeanListHandler<ShopBean>(ShopBean.class));
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return list;

    }
    public List<ShopBean> showShop(String star,String name){
        List<ShopBean> list = null;
        String sql = "SELECT * FROM s_shop where `name` like '%"+name +"%'";
        try {
            list = qr.query(sql, new BeanListHandler<ShopBean>(ShopBean.class));
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return list;

    }

    public ShopBean showShopById(String ids){
        ShopBean list = null;
        String sql = "SELECT * FROM s_shop where `id`=?";
        try {
            list = qr.query(sql,new BeanHandler<ShopBean>(ShopBean.class),ids);
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return list;

    }










    public int  getCount(){

        Mysqld.setCon(JdbcUtil.open());
        ResultSet rs = Mysqld.QueryData("select count(*) as res from s_shop;", null);

        try {
            while(rs.next()){
                   String res= rs.getString("res");
                   return Integer.parseInt(res);
            }
            rs.close();

        }catch (SQLException e){

        }
        return 0;


    }

    /**
     * 删除商品
     * @param id
     * @return
     */

    public int delShop(String id){
        int num=0;
        String sql = "delete  from  s_shop where id=?";
        try {
            num=qr.update(sql, id);
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return num;
    }


}
